package com.jinlejia.erp.ui.mine.contract;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.alibaba.fastjson.JSONObject;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivityAuditBinding;
import com.jinlejia.erp.utils.DataFilter;
import com.weight.CustomToast;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * 补充协议审核
 */
public class AuditSupplementAgreementActivity extends AppCompatActivity {
    JSONObject jsonObject = new JSONObject();

    private ActivityAuditBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_audit);
        binding.submit.setOnClickListener(this::onViewClicked);
        binding.backBtn.setOnClickListener(this::onViewClicked);
        binding.rejectLayout.setOnClickListener(this::onViewClicked);
        jsonObject.put("id", getIntent().getStringExtra("id"));
        jsonObject.put("state", 1);
        binding.inputContent.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;
            private int selectionStart;
            private int selectionEnd;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                temp = s;
            }

            @Override
            public void afterTextChanged(Editable s) {
                int number = 200 - s.length();
                binding.textCount.setText(number + "/200");
                selectionStart = binding.inputContent.getSelectionStart();
                selectionEnd = binding.inputContent.getSelectionEnd();
                if (temp.length() > 200) {
                    s.delete(selectionStart - 1, selectionEnd);
                    int tempSelection = selectionEnd;
                    binding.inputContent.setText(s);
                    binding.inputContent.setSelection(tempSelection);//设置光标在最后
                }
                jsonObject.put("reason", binding.inputContent.getText().toString());
            }
        });

        binding.rgMain.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.pass_radio) {
                binding.rejectLayout.setVisibility(View.GONE);
                jsonObject.put("state", 1);
            } else if (checkedId == R.id.reject_radio) {
                jsonObject.put("state", 2);
            }
        });

    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.reject_layout:
                //驳回原因选择
                Intent intent = new Intent(AuditSupplementAgreementActivity.this, RejectSelectReasonActivity.class);
                intent.putStringArrayListExtra("rejectReasonList", getIntent().getStringArrayListExtra("rejectReasonList"));
                startActivityForResult(intent, 100);
                break;
            case R.id.submit:
                //审核提交
                submit();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK) {
            ArrayList<String> list = data.getStringArrayListExtra("reason");
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.size(); i++) {
                sb.append(",");
                sb.append(list.get(i));
            }
            sb.deleteCharAt(0);
            binding.inputContent.setText(sb.toString());

        }
    }

    private void submit() {


        OkHttpUtils.postString()
                .url(ApiConfig.SUPPLY_AGREEMENT_AUDIT)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(jsonObject.toString())
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        call.cancel();
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(AuditSupplementAgreementActivity.this, response)) {
                            CustomToast.showToast(AuditSupplementAgreementActivity.this, "提交成功", 1500);
                            finish();
                        }
                    }
                });
    }
}
